package com.zxjbyte.yiyi.framework.web.filter;

import com.zxjbyte.yiyi.framework.common.constant.Constant;
import com.zxjbyte.yiyi.framework.common.util.ServletXUtil;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * HttpServletRequest 包装类，用于缓存请求体，支持重复读取 body。
 * @Author zhangxingjia
 * @Date 2023/3/17 20:25
 * @Version: 1.0
 */
public class YiyiHttpServletRequestWrapper extends HttpServletRequestWrapper {

    //没被包装过的HttpServletRequest
    private final HttpServletRequest origRequest;
    // 缓存body
    private byte[] body;

    public YiyiHttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
        this.origRequest = request;
    }

    @Override
    public BufferedReader getReader() throws IOException {
        String charset = getCharacterEncoding();
        if (charset == null) {
            charset = Constant.UTF_8;
        }
        return new BufferedReader(new InputStreamReader(this.getInputStream(), charset));
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        String contentType = super.getHeader(HttpHeaders.CONTENT_TYPE);
        if (contentType == null || contentType.toLowerCase().startsWith(MediaType.MULTIPART_FORM_DATA_VALUE.toLowerCase())) {
            return super.getInputStream();
        }

        if (body == null) {
            body = ServletXUtil.getBodyBytes(super.getRequest());
        }

        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body);

        return new ServletInputStream() {

            @Override
            public int read() {
                return byteArrayInputStream.read();
            }

            @Override
            public boolean isFinished() {
                return byteArrayInputStream.available() == 0;
            }

            @Override
            public boolean isReady() {
                return true; // 数据已经在内存中，总是准备好
            }

            @Override
            public int available() { //获取当前可从流中读取的字节数
                return byteArrayInputStream.available();
            }

            @Override
            public void setReadListener(ReadListener readListener) {
            }
        };
    }


    /**
     * 获取初始request
     *
     * @return HttpServletRequest
     */
    public HttpServletRequest getOrigRequest() {
        return origRequest;
    }

    /**
     * 获取初始request
     *
     * @param request request
     * @return HttpServletRequest
     */
    public static HttpServletRequest getOrigRequest(HttpServletRequest request) {
        if (request instanceof YiyiHttpServletRequestWrapper) {
            return ((YiyiHttpServletRequestWrapper) request).getOrigRequest();
        }
        return request;
    }
}
